/*******************************************************************************
 * Level Master 2000
 * Copyright (C) 2010 Christian Herzog
 * For more information see http://code.google.com/p/levelmaster2000/
 *
 * This file is licensed under the terms of the MIT license, see
 * http://www.opensource.org/licenses/mit-license.php for further information.
 ******************************************************************************/

package com.zog
{
    import com.pblabs.engine.components.TickedComponent;
    import com.pblabs.engine.PBE;
    import com.pblabs.engine.debug.Logger;
	
	import flash.events.Event;
	import flash.geom.Point;
    
    import com.pblabs.box2D.Box2DSpatialComponent;

	public class RacingLogic extends TickedComponent
	{
        //TODO: use PBE.callLater!!!1111
        private var resetPlayer:Boolean = false;
        private var markerIndex:int = 0;
        private var markerPosition:Point = null;
        private var markerRotation:Number = 0;

		protected override function onAdd():void
		{
			super.onAdd();
            owner.eventDispatcher.addEventListener(MarkerEvent.TYPE, onPassMarker);
            owner.eventDispatcher.addEventListener(StartAndFinishMarkerEvent.TYPE, onPassGoal);
		}
		
		protected override function onRemove():void
		{
			super.onRemove();
            owner.eventDispatcher.removeEventListener(MarkerEvent.TYPE, onPassMarker);
            owner.eventDispatcher.removeEventListener(StartAndFinishMarkerEvent.TYPE, onPassGoal);
		}
		
        public override function onTick(tickRate:Number):void
        {
            if(resetPlayer)
            {
                resetPlayer = false;
                var spatial:Box2DSpatialComponent = owner.lookupComponentByType(Box2DSpatialComponent) as Box2DSpatialComponent;
                if(spatial != null)
                {
                    spatial.rotation = markerRotation;
                    spatial.position = markerPosition; // .clone();
                    spatial.linearVelocity = new Point();
                }
              
            }
        }

        protected function onPassGoal(event:StartAndFinishMarkerEvent):void
        {
            if (markerIndex == 3
                || markerIndex == 0)
            {
                markerIndex = 0;
                markerPosition = event.position;
                markerRotation = event.rotation;
            }
            else
            {
                resetPlayer = true;
            }            
        }
        
        protected function onPassMarker(event:MarkerEvent):void
        {
            if ((markerIndex + 1) == event.index
                || markerIndex == event.index)
            {
                markerIndex = event.index;
                markerPosition = event.position;
                markerRotation = event.rotation;
            }
            else
            {
                resetPlayer = true;
            }
        }
	}
}